package com.inossem.warehousestorage_inossem.activity.refund_storage;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.refundStorage.RefundStorageSaleListAdapter;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageSaleListBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.refundStorage.RefundStorageSaleListRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

//销售退库列表
public class RefundStorageSaleListActivity extends BaseActivity {

    TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView saleRefundStorageList;

    private List<RefundStorageSaleListBean.SaleBodyBean> allData;

    private RefundStorageSaleListAdapter adapter;

    private String mSaleNumber, mContractNumber;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_refund_storage_sale_list);
        initView(RefundStorageSaleListActivity.this);
        initData(RefundStorageSaleListActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        saleRefundStorageList = (PullableListView) activity.findViewById(R.id.saleRefundStorageList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.saleRefundStorageListRefresh);
    }

    private void initData(Activity activity) {
        title.setText(getResources().getString(R.string.activity_refund_storage_sale_list_sales_restores_list));
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_main_title_short), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        saleRefundStorageList.setPullUpFlag(false);
        saleRefundStorageList.setPullDownFlag(false);
        mSaleNumber = "";
        allData = new ArrayList<>();
        addSaleRefundListData(activity, allData, saleRefundStorageList, true);
    }

    private void addSaleRefundListData(Context context, List<RefundStorageSaleListBean.SaleBodyBean> saleOutStorageList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new RefundStorageSaleListAdapter(context, saleOutStorageList, mSaleNumber, mContractNumber, (String saleNumber, String contractNumber) -> {
                String checkResult = check(saleNumber, contractNumber);
                if (checkResult == null) {
                    mSaleNumber = saleNumber;
                    mContractNumber = contractNumber;
                    getData(context, mSaleNumber, mContractNumber);
                } else {
                    Utils.middleToast(context, checkResult);
                }
            });
            listView.setAdapter(adapter);
        }
    }

    private String check(String saleNumber, String contractNumber) {
        String result = null;
        if (TextUtils.isEmpty(saleNumber) && TextUtils.isEmpty(contractNumber)) {
            result = getResources().getString(R.string.activity_refund_storage_sale_list_the_sales_number_and_contract_code_cannot_be_empty_at_the_same_time);
        }
        return result;
    }

    private void getData(Context context, String saleNumber, String contractNumber) {
        RefundStorageSaleListRequest.request(context, saleNumber, contractNumber, new GsonBeanListener<RefundStorageSaleListBean>() {
            @Override
            public void onSuccess(RefundStorageSaleListBean bean) {
                allData = bean.getBody();
                if (allData.isEmpty()) {
                    Utils.middleToast(context, getResources().getString(R.string.activity_in_storage_receive_list_no_message));
                }
                addSaleRefundListData(context, ActivityUtils.getShowData(allData, 0), saleRefundStorageList, true);
            }
        });
    }

    public interface RefundStorageSaleListener {
        void onNoDoubleClickListener(String saleNumber, String contractNumber);
    }

}
